package edu.hunau.cxb19.basic.class09;

import java.util.Arrays;

/**
 *
 * @author hb
 *
 *         2020-02-19 15:52:08
 */
public class BinarySearchDemo {
	public static void main(String[] args) {
		int[] arr = { 1, 3, 6, 23, 24, 34, 45, 68 };
		System.out.println(Arrays.toString(arr));
		binarySearch(arr, 45);
		
		Arrays.binarySearch(arr, 4);
		Arrays.sort(arr);
	}

	/**
	 * 二分查找
	 * 
	 * @param arr
	 * @param number
	 */
	public static void binarySearch(int[] arr, int number) {
		// 设置低位
		int low = 0;
		// 设置高位
		int high = arr.length - 1;
		boolean hasNumber = true;
		while (low <= high) {
			// 中间位置
			int mid = (low + high)>>>1 ;//等价于(low + high)/2
			if (number == arr[mid]) {
				System.out.println("找到数据...");
				hasNumber = false;
				break;
			} else if (arr[mid] > number) {
				high = mid - 1;
			} else {
				low = mid + 1;
			}
		}

		if (hasNumber) {
			System.out.println("没有该数据....");
		}

	}
}
